package com.shujia.java.day13.demo2;

import java.util.HashSet;

/**
 * 手机超市
 */
public class HashSetTest {
    public static void main(String[] args) {
        //创建集合
        HashSet hashSet = new HashSet();
        //创建手机对象
        Phone phone = new Phone("华为",6000,"P30");
        Phone phone2 = new Phone("华为",3000,"P20");
        Phone phone3 = new Phone("苹果",6000,"X");
        Phone phone1 = new Phone("苹果",12000,"MAX X");

        //添加
        hashSet.add(phone);
        hashSet.add(phone1);
        hashSet.add(phone2);
        hashSet.add(phone3);

        System.out.println("之前的价格");
        for (Object o : hashSet) {
            //类型转换
            Phone p= (Phone) o;
            //输出
            System.out.println(p.getName()+"的价格为"+p.getPrice());
        }
        System.out.println("之后的价格");
        //遍历hashset
        for (Object o:hashSet) {
            //类型转换
            Phone p= (Phone) o;
            //更改价格
            if (p.getPrice()<=10000){
                //重新赋值
                p.setPrice(p.getPrice()+1000);
            }
            //输出
            System.out.println(p.getName()+"的价格为"+p.getPrice());
        }
        //打印华为手机的个数
        //定义变量接收个数
        int sum=0;
        //判断集合中有几个华为手机
        for (Object o :hashSet){
            //类型转换
            Phone p= (Phone) o;
            if(p.getName().equals("华为")){
                sum++;
            }
        }
        System.out.println("华为手机的个数为:"+sum);

    }
}
